import time
import pyautogui
import time
import ctypes
import keyboard
import pygetwindow as gw
import getpass

import win32clipboard
from PIL import Image
from io import BytesIO
import os

import sn

def get_mac():
    import uuid
    return ''.join(['{:02x}'.format((uuid.getnode() >> ele) & 0xff) for ele in range(0, 8 * 6, 8)][::-1])


def copy_image_to_clipboard(path):
    image = Image.open(path)
    output = BytesIO()
    image.save(output, 'BMP')
    data = output.getvalue()[14:]
    output.close()
    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    win32clipboard.SetClipboardData(win32clipboard.CF_DIB, data)
    win32clipboard.CloseClipboard()

def wechat_init():
    system_scaling_factor = get_system_scaling_factor()

    # 先验证是否注册了
    if sn.snCheck() == False:
        return "没有购买或已经到期, 请先购买再使用"

    pyautogui.hotkey('ctrl', 'alt', 'w')

    time.sleep(0.5)

    wechat_window = None
    windows = gw.getAllWindows()
    for window in windows:
        if window.title == "微信":
            wechat_window = window
            break
    
    if wechat_window is not None:
        # 找到了微信窗口
        print("进入微信进程")
    else:
        # 没有找到微信窗口
        print("没有找到微信窗口")
        return "没有找到微信窗口"
    
    wechat_window.activate()
    #wechat_window.resizeTo(int(2000/system_scaling_factor), int(1600/system_scaling_factor))  # 设置窗口大小
    wechat_window.moveTo(0, 0)  # 设置窗口位置

    return True
# 微信朋友圈初期化
def moment_init():
    moment_window = None
    windows = gw.getAllWindows()
    for window in windows:
        if window.title == "朋友圈":
            moment_window = window
            break
    
    if moment_window is not None:
        print("进入朋友圈")
        return moment_window
    else:
        print("没有找到朋友圈")

def get_system_scaling_factor():
    # 获取用户32.dll模块
    user32 = ctypes.windll.user32
    # 调用GetDpiForSystem函数获取系统的DPI
    dpi_x = user32.GetDpiForSystem()
    # 计算缩放比例
    scaling_factor = dpi_x / 96.0
    # print("scaling_factor", scaling_factor)
    return scaling_factor

def getImgPath(imageName):
    if keyboard.is_pressed('shift'): exit()
    version = "3.9.7.25"

    current_path = os.path.abspath(__file__)
    directory = os.path.dirname(current_path)

    path = os.path.join(directory, 'images',  str(get_system_scaling_factor()), version, imageName)
    # print(path)
    return path

def getUserFilePath(file):
    if keyboard.is_pressed('shift'): exit()
    username = getpass.getuser()
    return r"C:\\Users\\" + username + "\\Documents\\" + file

def whileCenter(imagePath, msg=""):
    while True:
        img_path = getImgPath(imagePath)
        # print(img_path)
        image = pyautogui.locateCenterOnScreen(img_path, confidence=0.8)
        if image:
            return image
        else:
            if msg: print(msg)
            else: print("等待元素出现")
            time.sleep(1)

def moveToClick(xy):
    if keyboard.is_pressed('shift'): exit()
    # 如果xy是None
    if not xy: return False

    pyautogui.moveTo(xy[0], xy[1])
    time.sleep(0.1)
    # pyautogui.click()
    pyautogui.mouseDown()
    time.sleep(0.01) #or whatever you need, if even needed
    pyautogui.mouseUp()

# 获取某个图的坐标
def getPosCenter(img):
    if keyboard.is_pressed('shift'): exit()

    xy = pyautogui.locateCenterOnScreen(
        img, grayscale=True, confidence=0.9
    )
    return xy
